<template>
  <div class="bg">
    <div class="bigBox">
      <div class="topBox">
        <div
          class="topLeft"
          :class="[isActive ? 'active topLeft' : 'topLeft']"
          @click="changeTab(true)"
        >
          登录
        </div>
        <div
          class="topRight"
          :class="[!isActive ? 'active topRight' : 'topRight']"
          @click="changeTab(false)"
        >
          注册
        </div>
      </div>
      <div class="bottomBox">
        <el-form
          ref="loginFormRef"
          v-show="isActive"
          label-position="left"
          label-width="100px"
          :model="loginForm"
          style="min-width: 500px"
        >
          <el-form-item label="用户名">
            <el-input v-model="loginForm.username" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="loginForm.password" placeholder="请输入密码" />
          </el-form-item>
        </el-form>
        <el-form
          ref="regionFormRef"
          :rules="rules"
          v-show="!isActive"
          label-position="left"
          label-width="100px"
          :model="regionForm"
          style="min-width: 500px"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
              v-model="regionForm.username"
              placeholder="请输入用户名"
            />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="regionForm.password" placeholder="请输入密码" />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input
              v-model.number="regionForm.phone"
              placeholder="请输入手机号"
            />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="regionForm.email" placeholder="请输入邮箱" />
          </el-form-item>
        </el-form>
        <el-button
          type="primary"
          @click="onSubmit"
          style="
            font-size: 24px;
            font-weight: bold;
            text-align: center;
            background-color: #02aeff;
            border: none;
            width: 100%;
            height: 50px;
            border-radius: 25px;
            margin-top: 30px;
          "
          >{{ isActive ? '登录' : '注册' }}</el-button
        >
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { region } from '@/api/login'
import {router} from '@/routers'
import { useUserStore } from '@/store'

const isActive = ref(true)
const changeTab = (flag: boolean) => {
  isActive.value = flag
}

// const labelPosition = ref<FormProps['labelPosition']>('right')

const loginFormRef = ref(null)
const regionFormRef = ref(null)

const loginForm = reactive({
  username: '',
  password: '',
})

const regionForm = reactive({
  username: '',
  password: '',
  phone: '',
  email: '',
})

const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z0-9]{2,11}$/,
      message: '只能输入英文字母和数字且长度在2-11之间',
      trigger: ['change', 'blur'],
    },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z0-9]{6,11}$/,
      message: '只能输入英文字母和数字且长度在6-11之间',
      trigger: ['change', 'blur'],
    },
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    {
      pattern: /^1[3|4|5|7|8][0-9]{9}$/,
      message: '手机号格式不正确',
      trigger: ['change', 'blur'],
    },
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    {
      pattern: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/,
      message: '邮箱格式不正确',
      trigger: ['change', 'blur'],
    },
  ],
})

const userStore = useUserStore()

const onSubmit = async () => {
  if (isActive.value) {
    await loginFormRef.value.validate()
    const formData = new FormData()
    formData.append('client_id', 'c1')
    formData.append('client_secret', 'secret')
    formData.append('grant_type', 'password')
    formData.append('password', loginForm.password)
    formData.append('username', loginForm.username)
    // const res = await loginApi(formData)
    userStore.login(formData)
    setTimeout(() => {
      router.push('/user')
      resetForm()
    }, 500)
  } else {
    await regionFormRef.value.validate()
    const res = await region(regionForm)
    console.log(res)
    resetForm()
    isActive.value = true
  }

  // const cs1 = await cs()
  // console.log(cs1);

  // console.log(12);
}

// 重置表单
const resetForm = () => {
  if (isActive.value) {
    loginForm.username = ''
    loginForm.password = ''
    loginFormRef.value.resetFields()
  } else {
    regionForm.username = ''
    regionForm.password = ''
    regionForm.phone = ''
    regionForm.email = ''
    loginFormRef.value.resetFields()
  }
}
</script>

<style lang="less" scoped>
#app {
  width: 100%;
  height: 100%;
}
.bg {
  width: 100%;
  height: 100%;
  background: url(../../assets/img/login-bg.jpg) no-repeat center center;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
}
.bigBox {
  width: 600px;
  background-color: #fff;
  border-radius: 1.1rem;
  padding: 2.5rem;

  .topBox {
    height: 50px;
    display: flex;
    align-items: center;
    transition: all 0.5s;

    .topLeft,
    .topRight {
      flex: 1;
      font-size: 25px;
      font-weight: bold;
      color: #bdbdbd;
      cursor: pointer;
      border-bottom: 0.2rem solid #bdbdbd;
    }

    .active {
      color: #02aeff;
      border-color: #02aeff;
    }
  }

  .bottomBox {
    position: relative;
    height: 400px;
    display: flex;
    flex-direction: column;
    // align-items: center;
    justify-content: center;

    /deep/ .el-form-item__label,
    .el-input {
      height: 50px;
      line-height: 50px;
      font-size: 18px;
    }
  }
}

.el-form /deep/ .el-form-item__label {
  font-size: 20px;
  font-weight: bold;
}
</style>
